package com.shop.service.impl;

import com.shop.mapper.AdminMapper;
import com.shop.mapper.UserMapper;
import com.shop.model.Category;
import com.shop.model.Manager;
import com.shop.model.User;
import com.shop.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.lang.management.MemoryManagerMXBean;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    private UserMapper userMapper;

    public AdminServiceImpl(UserMapper userMapper) {
        this.userMapper=userMapper;
    }

    @Override
    public List<User> getAll() {

        return adminMapper.getAll();
    }



    @Override
    public Boolean update(Integer id, String account,String username) {
        User user=adminMapper.selectByPhone(account);
        if(user==null||(long)id==user.getId()) {
            adminMapper.update(id,account,username);
            return true;
        }
        return false;
    }

    @Override
    public void delete(Integer id) {
        adminMapper.delete(id);
    }

    @Override
    public Boolean add(String account) {
        User user=adminMapper.selectByPhone(account);
        if (user != null) {
            user.setUserType(1);
            userMapper.updateByPrimaryKeySelective(user);
        }
        else {
            User user1=new User();
            user1.setUsername("admin");
            user1.setPhone(account);
            user1.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
            user1.setUserType(1);
            user1.setCreateTime(LocalDateTime.now());
            user1.setUpdateTime(LocalDateTime.now());
            user1.setStatus(1);

            userMapper.insertSelective(user1);
        }
        return true;
    }
}
